package com.dayaokeji.rhythmschoolstudent.client.home.course

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Html
import android.text.TextUtils
import android.view.View
import com.dayaokeji.rhythmschoolstudent.R
import com.dayaokeji.rhythmschoolstudent.client.common.ActivityStackManager
import com.dayaokeji.rhythmschoolstudent.client.common.base.ActivityTemplateMethod
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity
import com.dayaokeji.rhythmschoolstudent.event.DeleteCourseFinishEvent
import com.dayaokeji.rhythmschoolstudent.event.ReqNewCourseEvent
import com.dayaokeji.rhythmschoolstudent.event.UpdateCourseEvent
import com.dayaokeji.rhythmschoolstudent.utils.*
import com.dayaokeji.rhythmschoolstudent.wiget.ConfirmDialog
import com.dayaokeji.rhythmschoolstudent.wiget.DeleteCourseConfirmDialog
import com.dayaokeji.server_api.ApiUtils
import com.dayaokeji.server_api.ServerResponse
import com.dayaokeji.server_api.api.CourseApi
import com.dayaokeji.server_api.domain.Course
import com.dayaokeji.server_api.domain.DeleteMember
import com.dayaokeji.server_api.domain.UserInfo
import kotlinx.android.synthetic.main.activity_exit_course.*
import org.greenrobot.eventbus.EventBus
import retrofit2.Call

/**
 * 退出课程
 */
class ExitCourseActivity : BaseActivity(), ActivityTemplateMethod {

    companion object {
        private const val EXTRA_COURSE = "course"
        fun navigation(cxt: Context,course: Course) {
            val intent = Intent(cxt, ExitCourseActivity::class.java)
            intent.putExtra(EXTRA_COURSE,course)
            cxt.startActivity(intent)
        }
    }

    private var userInfo: UserInfo? = null
    private var course: Course? = null

    private var title: String = ""

    private val courseApi: CourseApi by lazy { ApiUtils.getApi(CourseApi::class.java) }
    private var deleteMemberCall: Call<ServerResponse<Void>>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_exit_course)
        toolbar?.let {
            setSupportActionBar(it)
        }
        init()
    }

    override fun init() {
        course = intent.getSerializableExtra(EXTRA_COURSE) as? Course
        userInfo = UserUtils.getUserInfo()
        if (userInfo == null || course == null) {
            ToastUtils.info(R.string.data_incorrect)
            return
        }
        super.init()
    }

    override fun setupView() {

    }

    override fun setupViewData() {
        val info = UserUtils.getUserInfo()
        info?.let {
            tvUserName.text = it.name
        }
        title = "是否删除 <b><font color='#F63F3F' size='7'>[ ${course?.name} ]</font></b> ?"
        tvCourseTitle.text = Html.fromHtml(title)
    }

    fun exitCourseClick(view: View) {
        val lastPassword = PrefUtils.getLastPassword()
        if (TextUtils.isEmpty(lastPassword)) {
            ToastUtils.info("没有获取到本地存储密码，请退出重新登录")
            return
        }
        val loginPassword = etLoginPassword.text.toString().replace(" ","").trim()
        val courseName = etCourseName.text.toString().replace(" ", "").trim()
        if (!ObjectUtils.equals(course?.name,courseName)) {
            ToastUtils.info("请输入完整课程名")
            return
        }
        if (ObjectUtils.equals(loginPassword,lastPassword)) {
            delConfirmDialog()
        }else{
            ToastUtils.info("请输入密码或密码错误")
        }
    }

    /**
     * 退出课程确认对话框
     */
    private fun delConfirmDialog() {
        val confirmDialog = DeleteCourseConfirmDialog.newInstance(DeleteCourseConfirmDialog.ConfirmInfo(content = title))
        confirmDialog.setConfirmClickListener(object : DeleteCourseConfirmDialog.ConfirmClickListener {
            override fun click() {
                delMember()
            }
        })
        confirmDialog.show(supportFragmentManager,"delete_course")
    }

    /**
     * 退出课程
     */
    private fun delMember() {
        val deleteMember = DeleteMember()
        deleteMember.id = course?.id!!
        deleteMember.studentId = userInfo?.id
        deleteMemberCall = courseApi.deleteMember(deleteMember)
        deleteMemberCall?.enqueue(object : ServerCallback<Void>() {
            override fun onResponse(success: Boolean, response: ServerResponse<Void>?) {
                if (success) {
                    ToastUtils.success("删除成功")
                    EventBus.getDefault().post(UpdateCourseEvent())
                    EventBus.getDefault().post(ReqNewCourseEvent())
                    EventBus.getDefault().post(DeleteCourseFinishEvent())
                    ActivityStackManager.finish()
                }
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        deleteMemberCall?.cancel()
    }
}
